package com.fas.core.util;

import java.text.ParseException;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;
/**
 * 日期工具类
 * 
 *
 * @Copyright: Copyright (c) 2016 FAS 
 * @anthor yixiaoping<thinker1013@163.com>
 * @since 2016年1月23日 下午10:51:37
 * @version V1.0
 */
public class DateUtils {
	/**
	 * 得到几天前的时间
	 * @param dates
	 * @return
	 */
	public static Calendar getDate(int dates)
	{
    	Calendar cal = Calendar.getInstance();
    	cal.add(Calendar.DAY_OF_MONTH, dates);
    	return cal;
	}
	
	/**
	 * 得到几天前的时间
	 * @param dates
	 * @return
	 */
	public static Calendar getDateWithDate(int dates,Date d)
	{
    	Calendar cal = Calendar.getInstance();
    	cal.setTime(d);
    	cal.add(Calendar.DAY_OF_MONTH, dates);
    	return cal;
	}
	
    /**
     * 得到前几天
     */
    public static String getBeforeDate(int datas) throws ParseException{
    	Calendar cal = Calendar.getInstance();
    	cal.add(Calendar.DAY_OF_MONTH, -datas);
        return DateFormatFactory.getInstance("yyyy-MM-dd").format(cal.getTime());
    }
    /**
     * 得到当前时间前后几周
     */
    public static String getWeeksDate(int weeks) throws ParseException{
    	Calendar cal = Calendar.getInstance();
    	cal.add(Calendar.DAY_OF_MONTH, weeks*7);
        return DateFormatFactory.getInstance("yyyy-MM-dd").format(cal.getTime());
    }
    // 获得当前日期与本周一相差的天数
    private static int getMondayPlus(Calendar cd) {
        // 获得今天是一周的第几天，星期日是第一天，星期二是第二天......
        int dayOfWeek = cd.get(Calendar.DAY_OF_WEEK);
        if (dayOfWeek == 1) {
            return -6;
        } else {
            return 2 - dayOfWeek;
        }
    }
    
    // 获得周日的日期
    public static String getSunday(String date) throws ParseException {
    	Date d = DateFormatFactory.getInstance("yyyy-MM-dd").parse(date);
    	GregorianCalendar currentDate = new GregorianCalendar();
    	currentDate.setTime(d);
        int mondayPlus = getMondayPlus(currentDate);
        currentDate.add(GregorianCalendar.DATE, mondayPlus + 6);
        Date monday = currentDate.getTime();
        String preMonday = DateFormatFactory.getInstance("yyyy-MM-dd").format(monday);
        return preMonday;
    }
    
    // 获得星期一的日期
    public static String getMonday(String date) throws ParseException {
    	Date d = DateFormatFactory.getInstance("yyyy-MM-dd").parse(date);
    	GregorianCalendar currentDate = new GregorianCalendar();
    	currentDate.setTime(d);
        int mondayPlus = getMondayPlus(currentDate);
        currentDate.add(GregorianCalendar.DATE, mondayPlus);
        Date monday = currentDate.getTime();
        String preMonday = DateFormatFactory.getInstance("yyyy-MM-dd").format(monday);
        return preMonday;
    }
    
    /**
     * 得到当前时间六个月内的时间
     */
    public static String[] getBeforeMonth() throws ParseException{
    	String date[] = new String[6];
    	
    	for(int i=0;i<6;i++){
    		Calendar cal = Calendar.getInstance();
    		cal.add(Calendar.MONTH,-i);
    		date[i] =DateFormatFactory.getInstance("yyyy-MM").format(cal.getTime());
    	}
    	return date;
    } 
    
    /**
     * 得到当前时间前一周的时间
     * @return
     * @throws ParseException
     */
    public static String[] getBeforeDay() throws ParseException{
    	String date[] = new String[7];
    	for(int i=0;i<7;i++){
    		Calendar cal = Calendar.getInstance();
    		cal.add(Calendar.DAY_OF_MONTH, -(i+1));
    		date[i] =DateFormatFactory.getInstance("yyyy-MM-dd").format(cal.getTime());
    	}
    	return date;
    }
    
    public static void main(String args[]) throws ParseException
    {
    	//System.out.println(getMonday("2012-02-05"));
    	//System.out.println(getSunday("2012-02-05"));
    	//System.out.println(getMonday(getWeeksDate(-1)));
    	//System.out.println(getSunday(getWeeksDate(-1)));
    	for(int i=0;i<6;i++)
    	System.out.println(getBeforeMonth()[i]);
    	for(int i=0;i<7;i++)
        	System.out.println(getBeforeDay()[i]);
    }
}
